/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufjf.posjava.cm.model.DAO;

import br.ufjf.posjava.cm.model.Servico;
import br.ufjf.posjava.cm.util.GenericDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Fabíola
 */
public class ServicoDAO extends GenericDao<Servico, Integer> {

    @Override
    public void inserir(Servico obj) throws ClassNotFoundException, SQLException {
        String sql = "INSERT INTO servico (descricao,duracao,valor) VALUES (?,?,?)";

        ArrayList<String> lista = new ArrayList<String>();

        lista.add(obj.getDescricao());
        lista.add(Long.toString(obj.getDuracao()));
        lista.add(Double.toString(obj.getValor()));


        executaDML(sql, lista);
    }

    @Override
    public void alterar(Servico obj) throws ClassNotFoundException, SQLException {
        String sql = "UPDATE servico SET valor = ?, duracao = ?, descricao = ? where id = ?";

        ArrayList<String> lista = new ArrayList<String>();
        lista.add(Double.toString(obj.getValor()));
        lista.add(Long.toString(obj.getDuracao()));
        lista.add(obj.getDescricao());
        lista.add(obj.getIdServico().toString());

        executaDML(sql, lista);
    }

    @Override
    public void apagar(Servico obj) throws ClassNotFoundException, SQLException {
        String sql = "DELETE FROM servico WHERE id = ?";

        ArrayList<String> lista = new ArrayList<String>();
        lista.add(obj.getIdServico() + "");

        executaDML(sql, lista);

    }

    @Override
    public Servico pesquisarChave(Integer obj) throws ClassNotFoundException, SQLException {
        String sql = "SELECT * FROM servico WHERE id = ?";
        ArrayList<String> lista = new ArrayList<String>();
        lista.add(obj.toString());

        ResultSet rs = executaSql(sql, lista);
        if (rs.next()) {
            Servico servico = new Servico(rs.getInt("id"), rs.getString("descricao"), rs.getLong("duracao"), rs.getDouble("valor"));
            return servico;
        } else {
            return null;
        }
    }

    @Override
    public ArrayList<Servico> buscarTodos() throws ClassNotFoundException, SQLException {
        String sql = "SELECT * FROM servico";
        ArrayList<String> lista = new ArrayList<String>();
        ArrayList<Servico> listaS = new ArrayList<Servico>();
        ResultSet rs = executaSql(sql, lista);
        while (rs.next()) {
            Servico s = new Servico(rs.getInt("id"), rs.getString("descricao"), rs.getLong("duracao"), rs.getDouble("valor"));
            listaS.add(s);
        }
        return listaS;
    }

    public ArrayList<Servico> buscarServico(String descricao) throws ClassNotFoundException, SQLException {
        String sql = "SELECT * FROM servico where descricao LIKE '%" + descricao + "%'";

        ArrayList<String> lista = new ArrayList<String>();
        ArrayList<Servico> listaS = new ArrayList<Servico>();
        ResultSet rs = executaSql(sql, lista);
        while (rs.next()) {
            Servico s = new Servico(rs.getInt("id"), rs.getString("descricao"), rs.getLong("duracao"), rs.getDouble("valor"));

            listaS.add(s);
        }
        return listaS;
    }

    public ArrayList<Servico> buscarValorDoServico(Integer id) throws ClassNotFoundException, SQLException {
        String sql = "select * from servico where id = ?";
        ArrayList<String> lista = new ArrayList<String>();
        lista.add(id.toString());
        ArrayList<Servico> servicos = new ArrayList<Servico>();
        ResultSet rs = executaSql(sql, lista);
        while (rs.next()) {
            Servico sv = new Servico(rs.getInt("id"), rs.getString("descricao"), rs.getLong("duracao"), rs.getDouble("valor"));
            servicos.add(sv);
        }
        return servicos;

    }
}
